<template>
  <div class="set">
    <Row type="flex"
         align="middle"
         justify="center"
         class="set_ct">
      <Col>
        <Form ref="formDynamic"
            :model="formDynamic"
            :rules="ruleValidate"
            :label-width="120">
            <FormItem label="投币出卡比："
                  prop="coinCardRatio">
              <Input :maxlength="4"
                    @on-keyup="changeinput('coinCardRatio')"
                    v-model="formDynamic.coinCardRatio"
                    @on-blur="changeinput('coinCardRatio')"
                    style="width:280px"
                    placeholder="输入范围1~1000之间"></Input>
            </FormItem>
            <FormItem label="单卡平均赠币数："
                  prop="cardAverageGift">
              <Input :maxlength="4"
                    @on-keyup="changeinput('cardAverageGift')"
                    v-model="formDynamic.cardAverageGift"
                    @on-blur="changeinput('cardAverageGift')"
                    style="width:280px"
                    placeholder="输入范围1~1000之间"></Input>
            </FormItem>
            <FormItem label="每盒卡片张数："
                  prop="boxCardNum">
              <Input :maxlength="4"
                    @on-keyup="changeinput('boxCardNum')"
                    v-model="formDynamic.boxCardNum"
                    @on-blur="changeinput('boxCardNum')"
                    style="width:280px"
                    placeholder="输入范围1~1000之间"></Input>
            </FormItem>
            <FormItem>
              <Button type="primary"
                      @click="handleSubmit"
                      class="button">确定</Button>
              <Button @click="closeModal"
                      class="button btn1">取消</Button>
            </FormItem>
        </Form>
      </Col>
    </Row>
  </div>
</template>

<script>
import './index.less'
import { routeEqual } from '@/libs/util'
import { getDicQuery, setDicSave } from '@/api/setting'
export default {
  data () {
    return {
      formDynamic: {
        coinCardRatio: '85',
        cardAverageGift: '5',
        boxCardNum: '100'
      },
      ruleValidate: {
        coinCardRatio: { required: true, message: '投币出卡比不能为空', trigger: 'blur' },
        cardAverageGift: { required: true, message: '单卡平均赠币数不能为空', trigger: 'blur' },
        boxCardNum: { required: true, message: '每盒卡片张数不能为空', trigger: 'blur' }
      },
      paramter: {
        'merchantId': this.$store.state.user.merchantId,
        'msType': '0'
      }
    }
  },
  methods: {
    changeinput (prop) {
      this.formDynamic[prop] = this.formDynamic[prop].replace(/[^\d]/g, '').replace(/^0{1,}/g, '')
      let value = this.formDynamic[prop]
      if (value > 1000) {
        this.formDynamic[prop] = value.substr(0, value.length - 1)
      }
    },
    handleSubmit () {
      this.$refs.formDynamic.validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.paramter, {
            coinCardRatio: this.formDynamic.coinCardRatio,
            cardAverageGift: this.formDynamic.cardAverageGift,
            boxCardNum: this.formDynamic.boxCardNum
          })
          setDicSave(data).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success('设置成功!')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'global_setting'
        })
      })
    }
  },
  mounted () {
    let obj = Object.assign({}, this.paramter, { 'dictKeys': 'coinCardRatio,cardAverageGift,boxCardNum' })
    getDicQuery(obj).then((res) => {
      if (res.data.status === 200) {
        res.data.data.forEach(ele => {
          if (ele.hasOwnProperty('coinCardRatio') && ele.coinCardRatio !== 0) {
            this.formDynamic.coinCardRatio = ele.coinCardRatio
          }
          if (ele.hasOwnProperty('cardAverageGift') && ele.cardAverageGift !== 0) {
            this.formDynamic.cardAverageGift = ele.cardAverageGift
          }
          if (ele.hasOwnProperty('boxCardNum') && ele.boxCardNum !== 0) {
            this.formDynamic.boxCardNum = ele.boxCardNum
          }
        })
      } else {
        this.$Message.error(`${res.data.error}`)
      }
    })
  }
}
</script>

<style lang="less" scoped>

</style>
